package priv.bestbeat.cloud.common.configuration;

import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import priv.bestbeat.cloud.common.properties.JWTProperties;

import java.util.Optional;

/**
 * @author 张渠钦
 * @date 2021/8/9 14:40
 */
@Configuration
@EnableConfigurationProperties(JWTProperties.class)
public class JWTConfiguration {

    private static JWTProperties properties;
    private static Algorithm algorithm;
    private static String issuer;
    private static Long expiresMinutes;

    public JWTConfiguration(@Autowired JWTProperties properties) {
        JWTConfiguration.properties = properties;
        Optional.ofNullable(properties.getSecret()).ifPresent(secret -> JWTConfiguration.algorithm = Algorithm.HMAC256(secret));
        Optional.ofNullable(properties.getIssuer()).ifPresent(issuer -> JWTConfiguration.issuer = issuer);
        Optional.ofNullable(properties.getExpiresMinutes()).ifPresent(expiresMinutes -> JWTConfiguration.expiresMinutes = expiresMinutes);
    }

    public static Algorithm getAlgorithm() {
        return algorithm;
    }

    public static String getIssuer() {
        return issuer;
    }

    public static long getExpiresMinutes() {
        return expiresMinutes;
    }

}
